package chapter08

/**
  *
  * @author Pop
  * @date 2022/7/5 22:12
  */
object Test06_PartialFunction {
  def main(args: Array[String]): Unit = {
    val tuples: List[(String, Int)] = List(("a",12),("b",35),("c",2))

    // 将值变成2倍
    tuples.map(tuples=>(tuples._1,tuples._2*2))

    // 模式匹配

    tuples.map(tuples=>{
        tuples match {
          case (word,count) => (word,count*2)
        }
    })

    // 简化，这就是偏函数

    tuples.map(
      {
        case (word,count) => (word,count*2)
      }
    )

    // 偏函数
    val positiveAbs:PartialFunction[Int,Int] ={
      case x if x>0 => -x
    }

    val negativeAbs:PartialFunction[Int,Int] ={
      case x if x>0 => x
    }

    // 偏函数的组合起来了
    def abs(x:Int):Int = (positiveAbs orElse negativeAbs)(x)

  }
}
